<template>
  <div class="WakeUpDialog">
    <el-dialog
      title="礼厅信息维护(守灵)"
      :visible.sync="$parent.DialogWakeUp"
      v-dialogDrag
      width="80%"
      @close="closeDialog()"
    >
      <el-form
        :model="copyWakeUpInfo.wakeMourn"
        :rules="rules"
        :inline="true"
        label-width="120px"
        ref="WakeUp"
        :disabled="saveFlag"
      >
        <el-row :gutter="5">
          <el-form-item label="守灵厅类型：" prop="hallTypeCode">
            <el-select
              v-model="copyWakeUpInfo.wakeMourn.hallTypeCode"
              @change="changeTypeCodeAsRuleItem(copyWakeUpInfo.wakeMourn.hallTypeCode)"
              :disabled="ProcessStateWakeUpOld==3"
              placeholder="请选择守灵厅类型"
              style="width: 120px;"
            >
              <el-option label="全部" value=""></el-option>
              <el-option
                v-for="(item,index) in ruleItems"
                :key="index"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState>0?true:false"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="开始时间：" prop="appointmentInTime">
            <el-date-picker
              v-model="copyWakeUpInfo.wakeMourn.appointmentInTime"
              format="yyyy-MM-dd HH:mm"
              type="datetime"
              :picker-options="pickerOptions0"
              @blur="loadChargeWithRule()"
              :disabled="(ProcessStateWakeUpOld==3)"
              placeholder="选择日期时间"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
          <el-form-item label="结束时间：">
            <el-date-picker
              v-model="copyWakeUpInfo.wakeMourn.appointmentOutTime"
              format="yyyy-MM-dd HH:mm"
              type="datetime"
              :picker-options="pickerOptions0"
              @blur="loadChargeWithRule()"
              :disabled="ProcessStateWakeUpOld==3"
              placeholder="选择日期时间"
              style="width: 100%;"
            ></el-date-picker>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="礼厅名称：" prop="hallCode">
            <el-input v-model="selectSave.hallName" :disabled="true">
              <span slot="append" style="cursor: pointer;" @click="select()">选择</span>
            </el-input>
          </el-form-item>
          <el-form-item label="守灵费用：">
            <el-input
              v-model="copyWakeUpInfo.wakeMourn.charges.businessCharge.itemCharge"
              type="number"
              :disabled="ProcessStateWakeUpOld==3"
            ></el-input>
          </el-form-item>
          <el-form-item label="守灵状态：" prop="processStateCode">
            <el-select
              v-model="copyWakeUpInfo.wakeMourn.processStateCode"
              placeholder="请选择"
              onkeydown="return (event.keyCode!=8)"
              @change="changeWakeUpProcessStateCode(copyWakeUpInfo.wakeMourn.processStateCode)"
              :disabled="ProcessStateWakeUpOld==3"
              style="width: 120px;"
            >
              <el-option
                v-for="(item,index) in  hallStatus"
                :key="index"
                :label="item.dataText"
                :value="item.dataCode"
                :disabled="item.disableState>0?true:false"
              />
            </el-select>
          </el-form-item>
        </el-row>
        <el-row :gutter="5">
          <el-form-item label="规则描述：">
            <el-input
              type="textarea"
              v-model="copyWakeUpInfo.wakeMourn.ruleRemark"
              :readonly="true"
              style="width: 808px;"
            ></el-input>
          </el-form-item>
        </el-row>

        <el-row :gutter="5">
          <el-form-item label="备注：">
            <el-input
              type="textarea"
              v-model="copyWakeUpInfo.wakeMourn.remark"
              style="width: 808px;"
            ></el-input>
          </el-form-item>
        </el-row>

        <div class="tablewrapper">
          <div class="table">
            <div class="title">
              <span>所有物品、服务</span>
              <!--<el-input v-model="search" placeholder="搜索物品/服务" style="width: 200px;float: right;"></el-input>-->
            </div>
            <el-table
              :data="copyGoodServices"
              :disabled="saveFlag"
              :header-cell-style="{background:'rgb(246,247,251)',color:'#606266'}"
              border
              style="width: 99%;"
            >
              <el-table-column
                align="center"
                prop="itemName"
                label="收费项目"
                sortable
                min-width="120"
                column-key="serviceItemId"
              ></el-table-column>
              <el-table-column prop="itemPrice" align="right" label="单价" min-width="80">
                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-if="row.asFixPrice !=1"
                    type="number"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                    v-model="row.itemPrice"
                  />
                  <label class="edit-cell" v-if="row.asFixPrice ==1">{{ row.itemPrice }}</label>
                </template>
              </el-table-column>
              <el-table-column prop="itemNumber" align="right" label="数量" min-width="50">
                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-model="row.itemNumber"
                    type="number"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                  />
                </template>
              </el-table-column>
              <el-table-column prop="itemUnit" align="center" label="单位" min-width="50"></el-table-column>
              <el-table-column prop="itemCharge" align="right" label="总金额" min-width="50">
                <template
                  slot-scope="{row}"
                  v-if="Number(row.itemPrice)>=0 &&Number(row.itemNumber)>=0&&Number(row.discountCharge)>=0"
                >
                  {{
                    Number((row.itemPrice.match(/^\d+(?:.\d{0,2})?/)) * (row.itemNumber.match(/^\d+(?:.\d{0,2})?/))).toFixed(2)
                  }}
                </template>
              </el-table-column>
              <el-table-column prop="discountCharge" align="right" label="优惠" min-width="50">
                <template slot-scope="{row}">
                  <input
                    class="edit-cell"
                    v-model="row.discountCharge"
                    onkeyup="value= (value.match(/^\d*(\.?\d{0,2})/g)[0]) || null"
                  />
                </template>
              </el-table-column>
              <el-table-column align="center" prop="itemSurplusNum" label="物品剩余"></el-table-column>
              <el-table-column label="操作" align="center" width="100">
                <template slot-scope="{$index}">
                  <el-button
                    @click.native.prevent="AddRow($index,copyGoodServices)"
                    type="text"
                    size="small"
                  >追加
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--<p class="tablecont">单价、数量、优惠三个字段值允许工作人员直接更改，更改完成后可通过“追加”按钮进行费用追加。</p>-->
          </div>
          <div class="table">
            <h3 class="title">已追加物品、服务</h3>
            <el-table
              :data="copyWakeUpInfo.wakeMourn.charges.goodsServices"
              border
              style="width: 99%;"
            >
              <el-table-column align="center" prop="itemName" label="收费项目" min-width="100"></el-table-column>
              <el-table-column prop="itemPrice" align="right" label="单价" min-width="70"></el-table-column>
              <el-table-column prop="itemNumber" align="right" label="数量" min-width="50"></el-table-column>
              <el-table-column prop="itemUnit" align="center" label="单位" min-width="50"></el-table-column>
              <el-table-column prop="itemCharge" align="right" label="项目金额" min-width="80"></el-table-column>
              <el-table-column prop="discountCharge" align="right" label="优惠" min-width="50"></el-table-column>
              <el-table-column prop="benefitCharge" align="right" label="减免" min-width="50"></el-table-column>
              <el-table-column prop="asSettled" align="center" label="结算状态" width="100">
                <template slot-scope="{row}">
                  <span v-if="row.asSettled==0">未结算</span>
                  <span v-else-if="row.asSettled==1">已结算</span>
                </template>
              </el-table-column>
              <el-table-column label="操作" align="center">
                <template slot-scope="{row,$index}">
                  <el-button
                    v-if="row.asSettled==0"
                    @click.native.prevent="deleteRow($index,copyWakeUpInfo.wakeMourn.charges.goodsServices)"
                    type="text"
                    size="small"
                  >删除
                  </el-button>
                </template>
              </el-table-column>
            </el-table>
            <!--<p class="tablecont">此区域费用信息不允许更改，仅未结算的费用可以进行删除操作。-->
            <!--无论是新追加的费用还是删除的费用，均在保存操作后生效。-->
            <!--</p>-->
          </div>
        </div>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: right">
        <el-button type="primary" @click="Save()" :disabled="saveFlag">保 存</el-button>
        <el-button type="primary" plain @click="$parent.DialogWakeUp = false">取 消</el-button>
      </div>
      <el-dialog title="礼厅名称" :visible.sync="centerDialogVisible" width="50%" center>
        <div class="wrapper">
          <div class="box-card" v-if="wakeHalls.length>0" v-for="(n,m) in wakeHalls" :key="m">
            <!--<el-radio v-model="selectname"  v-for="(n,index) in wakeHalls" :disabled="n.businessCode?true:false" :label="n" border size="medium">{{n.hallCode}}</el-radio>-->

            <div
              v-if="n.disableState==1"
              class="item repair"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"
            >
              <h2 v-if="n.hallName">礼厅名称：{{ n.hallName }}</h2>
              <h2 v-if="n.hallCode">礼厅类型：{{ n.hallType }}</h2>
            </div>
            <div
              v-if="n.disableState==0 && !n.businessId"
              class="item free"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"
            >
              <h2 v-if="n.hallName">礼厅名称：{{ n.hallName }}</h2>
              <h2 v-if="n.nextInTime">下一任务：{{ n.nextInTime }}</h2>
              <h2 v-if="n.hallCode">礼厅类型：{{ n.hallType }}</h2>
            </div>
            <div
              v-if="n.disableState==0 && n.businessId>0"
              class="item sale"
              :class="{selected:n.flag}"
              @click.stop="mournSelect(n,m)"
            >
              <h2 v-if="n.hallName">礼厅名称：{{ n.hallName }}</h2>
              <h2 v-if="n.deadName">逝者姓名：{{ n.deadName }}</h2>
              <h2 v-if="n.nextInTime">下一任务：{{ n.nextInTime }}</h2>
            </div>
          </div>
        </div>
        <span slot="footer" class="dialog-footer" style="text-align: right;">
          <el-button type="primary" @click="showSelectName()">确 定</el-button>
          <el-button type="primary" plain @click="centerDialogVisible = false">取 消</el-button>
        </span>
      </el-dialog>
    </el-dialog>
  </div>
</template>

<script>
import * as Type from "@/api/service";
import * as mournService from "@/api/mournService";
import {validPhone} from "@/utils/validate";
import {dataFormate} from "@/utils/index";
import {toFixed} from "@/utils/tofixed";

export default {
  name: "WakeUpDialog",
  props: {
    WakeUpInfo: {
      type: Object,
      default: function () {
        return {
          wakeMourn: {
            id: 0,
            businessCode: "",
            businessType: "",
            hallTypeCode: "",
            hallId: 0,
            appointmentInTime: "",
            appointmentOutTime: "",
            version: 0,
            calculateRule: "",
            charges: {
              businessCharge: {
                id: 0,
                serviceItemId: 0,
                itemPrice: 0,
                itemNumber: 0,
                itemUnit: "",
                itemCharge: 0,
                discountCharge: 0,
                benefitCharge: 0,
                realityCharge: 0,
                asSettled: 0,
                version: 0,
              },
              goodsServices: [],
            },
          },
        };
      },
    },
    DialogWakeUp: {
      type: Boolean,
      default: false,
    },
    WakeUpAddFlag: {
      //true为新增
      type: Boolean,
      default: false,
    },
    ruleItems: {
      type: Array,
    }, //守灵状态
    hallStatus: {
      type: Array,
    },
    ProcessStateWakeUpOld: {
      //守灵旧状态
      type: Number,
    }, //收费规则
  },
  data() {
    return {
      dictionary: {},
      rules: {
        hallTypeCode: [
          {required: true, message: "请选择类型", trigger: "blur"},
        ],
        appointmentInTime: [
          {required: true, message: "请选择开始时间", trigger: "blur"}, //这里需要用到全局变量
        ],

        hallCode: [
          {required: true, message: "请选择礼厅名称", trigger: "blur"},
        ],
      },
      formLabelWidth: "120px",
      //开始时间小于等于今天可选
      pickerOptions0: {
        disabledDate(time) {
          return time.getTime() < Date.now() - 8.64e7;
        },
      },
      saveFlag: false,
      copyGoodServices: [],
      centerDialogVisible: false,
      wakeHalls: [], //根据礼厅类型加载礼厅信息
      selectname: {}, //选中礼厅
      selectSave: {}, //点击确定时选中礼厅显示在界面上
      copyWakeUpInfo: this.WakeUpInfo,
      ruleItem: {}, //收费规则
      onpresscTime: 0, //  阻止短时间内连续点击
    };
  },
  created() {
    // this.$store
    //   .dispatch("user/getdictionary")
    //   .then((res) => {
    //     //获取字典数据
    //     this.dictionary = res;
    //     this.$set(this.dictionary, res);
    //   })
    //   .catch(() => console.log("promise catch err"));
  },
  watch: {
    "$parent.DialogWakeUp"() {
      this.copyWakeUpInfo = this.WakeUpInfo;
      this.copyGoodServices = JSON.parse(
        JSON.stringify(this.WakeUpInfo.serviceItems.use_in_mourn_ceremony)
      ); //复制
      if (!this.WakeUpAddFlag) {
        if (
          typeof this.copyWakeUpInfo.wakeMourn.appointmentOutTime == "string" &&
          this.copyWakeUpInfo.wakeMourn.appointmentOutTime.indexOf("-") > -1
        ) {
          this.copyWakeUpInfo.wakeMourn.appointmentOutTime = this.copyWakeUpInfo.wakeMourn.appointmentOutTime.replace(
            /-/g,
            "/"
          );
        }
        if (
          typeof this.copyWakeUpInfo.wakeMourn.appointmentInTime == "string" &&
          this.copyWakeUpInfo.wakeMourn.appointmentInTime.indexOf("-") > -1
        ) {
          this.copyWakeUpInfo.wakeMourn.appointmentInTime = this.copyWakeUpInfo.wakeMourn.appointmentInTime.replace(
            /-/g,
            "/"
          );
        }
      }
      this.$parent.DialogWakeUp = this.DialogWakeUp;
      if (this.copyWakeUpInfo.wakeMourn.hallCode && !this.WakeUpAddFlag) {
        //如果名字存在并且为编辑状态
        this.selectSave.hallCode = this.copyWakeUpInfo.wakeMourn.hallCode;
        this.selectSave.hallId = this.copyWakeUpInfo.wakeMourn.hallId;
        this.selectSave.hallTypeCode = this.copyWakeUpInfo.wakeMourn.hallTypeCode;
        this.selectSave.hallName=this.copyWakeUpInfo.wakeMourn.hallName;
      }
      if (this.$parent.DialogWakeUp == false) {
        this.selectname = {};
        this.selectSave = {};
      }
    },
  },
  methods: {
    //当类型改变时，查找适用计算规则
    changeTypeCodeAsRuleItem(code) {
      if (!code) {
        //this.$message("code值无效");
        return;
      }
      //判断并选择费用
      if (this.ruleItems) {
        for (let i = 0; i < this.ruleItems.length; i++) {
          let item = this.ruleItems[i];
          //选中code'
          if (item.dataCode == code) {
            //服务项目id
            this.copyWakeUpInfo.wakeMourn.charges.businessCharge.serviceItemId =
              item.serviceItemId;
            //服务项目价格
            this.copyWakeUpInfo.wakeMourn.charges.businessCharge.itemPrice =
              item.itemPrice;

            //规则编码
            this.copyWakeUpInfo.wakeMourn.ruleCode = item.ruleCode;
            //规则数量
            this.copyWakeUpInfo.wakeMourn.ruleNumber = item.itemNumber;
            //计量单位
            this.copyWakeUpInfo.wakeMourn.ruleUnit = item.itemUnit;

            //规则描述
            this.copyWakeUpInfo.wakeMourn.ruleRemark = item.ruleDescribe;

            // 计算规则编码
            this.ruleItem.ruleCode = item.ruleCode;
            // 计算规则单价
            this.ruleItem.itemPrice = item.itemPrice;
            // 计算规则参数
            this.ruleItem.ruleParams = item.ruleParams;

            this.loadChargeWithRule();
            break;
          }
        }
      }
    },
    //计算业务费用调用
    loadChargeWithRule() {
      if (!this.ruleItem) {
        return;
      }
      if (this.copyWakeUpInfo.wakeMourn.appointmentInTime=="")
      {
        return;
      }
      if (this.copyWakeUpInfo.wakeMourn.appointmentOutTime=="")
      {
        return;
      }
      let ruleData = JSON.parse(this.ruleItem.ruleParams);
      ruleData.itemPrice = this.ruleItem.itemPrice;
      ruleData.appointmentInTime = dataFormate(
        new Date(this.copyWakeUpInfo.wakeMourn.appointmentInTime)
      );
      ruleData.appointmentOutTime = dataFormate(
        new Date(this.copyWakeUpInfo.wakeMourn.appointmentOutTime)
      );
      let startTimeLong = new Date(
        this.copyWakeUpInfo.wakeMourn.appointmentInTime
      ).getTime();
      let endTimeLong = new Date(
        this.copyWakeUpInfo.wakeMourn.appointmentOutTime
      ).getTime();

      if (startTimeLong >= endTimeLong) {
        this.copyWakeUpInfo.wakeMourn.appointmentOutTime = "";
        this.$message("结束时间不能小于等于开始时间!.");
        return;
      }

      let postData = {
        ruleCode: this.ruleItem.ruleCode,
        ruleData: JSON.stringify(ruleData),
      };
      Type["loadChargeWithRule"](postData)
        .then((response) => {
          let result = response;
          const {data} = result;
          if (!data) {
            this.$message("费用计算信息出错.");
          }

          this.copyWakeUpInfo.wakeMourn.charges.businessCharge.itemCharge =
            data.itemCharge;
          //规则编码
          this.copyWakeUpInfo.wakeMourn.ruleCode = data.ruleCode;
          //规则数量
          this.copyWakeUpInfo.wakeMourn.ruleNumber = data.ruleNumber;
          //计量单位
          this.copyWakeUpInfo.wakeMourn.ruleUnit = data.ruleUnit;

          //规则描述
          this.copyWakeUpInfo.wakeMourn.ruleRemark = data.ruleRemark;
          this.$forceUpdate();

          //数据读取成功
        })
        .catch((error) => {
          this.$message(error);
        });
    },
    //级联选择
    handleChange(value) {
      // console.log(value);
    },
    //保存
    Save() {
      if (Date.now() - this.onpresscTime > 2000) {
        this.onpresscTime = Date.now();
        console.log(Date.now());
      } else {
        return;
      }
      let hallTypeCode =
        typeof this.copyWakeUpInfo.wakeMourn.hallTypeCode == "number" &&
        this.copyWakeUpInfo.wakeMourn.hallTypeCode >= 0;
      let appointmentInTime = this.copyWakeUpInfo.wakeMourn.appointmentInTime;

      if (!hallTypeCode || !appointmentInTime || !this.selectSave.hallCode) {
        this.$message("信息未填写完整");
        return;
      }
      let obj = JSON.parse(JSON.stringify(this.copyWakeUpInfo.wakeMourn));
      obj.charges.goodsServices.forEach((item, index) => {
        if (item.asSettled == 1) {
          obj.charges.goodsServices.splice(index, 1);
        }
      });
      obj.hallCode = this.selectSave.hallCode;
      //守灵厅加载
      if (this.WakeUpAddFlag) {
        obj.id = 0;
        obj.hallTypeCode = obj.hallTypeCode;
        obj.hallId = obj.hallId;
        obj.appointmentInTime = dataFormate(new Date(obj.appointmentInTime));
        obj.appointmentOutTime = dataFormate(new Date(obj.appointmentOutTime));
        obj.realityInTime = "";
        obj.realityOutTime = "";
        obj.randomCode = "";
        obj.chargeId = "";
        obj.businessType = "wake_mourn_type";
        obj.version = 0;
        obj.charges.businessCharge.id = 0;
        obj.charges.businessCharge.discountCharge = 0;
        obj.charges.businessCharge.benefitCharge = 0;
        obj.charges.businessCharge.asSettled = 0;
        obj.charges.businessCharge.chargeSettleId = 0;
        obj.charges.businessCharge.randomCode = 0;
        obj.charges.businessCharge.version = 0;
        obj.charges.businessCharge.chargeTime = "";
        obj.charges.businessCharge.businessCode = obj.businessCode;
        obj.charges.businessCharge.itemCharge = toFixed(
          Number(
            this.copyWakeUpInfo.wakeMourn.charges.businessCharge.itemCharge
          ),
          2
        );
        obj.charges.businessCharge.realityCharge = toFixed(
          Number(
            this.copyWakeUpInfo.wakeMourn.charges.businessCharge.itemCharge
          ),
          2
        );
      }
      if (!this.WakeUpAddFlag) {
        //时间格式
        obj.appointmentInTime = dataFormate(new Date(obj.appointmentInTime));
        obj.appointmentOutTime = dataFormate(new Date(obj.appointmentOutTime));
      }
      if (obj.charges.goodsServices.length > 0) {
        obj.charges.goodsServices.forEach((item, index) => {
          item.itemPrice = toFixed(item.itemPrice, 2);
          item.itemNumber = toFixed(item.itemNumber, 2);
          item.discountCharge = toFixed(item.discountCharge, 2);
          item.itemCharge = toFixed(item.itemCharge, 2);
        });
      }
      let postData = obj;
      this.saveFlag = true;
      mournService["saveWakeMourn"](postData)
        .then((response) => {
          let result = response;
          const {data} = result;
          if (!data) {
            this.saveFlag = false;
            this.$message("数据保存失败");
            return;
          }
          //数据读取成功
          this.copyWakeUpInfo.wakeMourn = data;
          this.saveFlag = false;
          this.$message("保存数据成功");
          this.$parent.DialogWakeUp = false;
        })
        .catch((error) => {
          this.$message(error);
          this.saveFlag = false;
        });
    },
    //追加守灵服务明细
    AddRow(index) {
      let row = this.copyGoodServices[index];
      if (index >= 0 && row) {
        row.itemPrice = Number(row.itemPrice).toFixed(2);
        row.itemNumber = Number(row.itemNumber).toFixed(2);
        row.itemCharge = Number(row.itemPrice * row.itemNumber).toFixed(2); //计算项目金额
        row.discountCharge = Number(row.discountCharge).toFixed(2);
        let numberValidate =
          Number(row.itemPrice) >= 0 &&
          Number(row.itemNumber) >= 0 &&
          Number(row.discountCharge) >= 0;
        if (
          !numberValidate ||
          !Number(row.itemCharge) ||
          Number(row.itemCharge) < Number(row.discountCharge)
        ) {
          this.$message("数值错误，请重新填写");
          Object.assign(
            row,
            this.copyWakeUpInfo.serviceItems.use_in_mourn_ceremony[index]
          );
          return true;
        }
        // 减免金额
        row.benefitCharge = 0;
        row.asSettled = 0;
        //子项目的serviceItemId=id;
        row.serviceItemId = row.id;
        //新增的id为0
        row.id = 0;

        let list = JSON.parse(JSON.stringify(this.copyGoodServices));
        this.copyWakeUpInfo.wakeMourn.charges.goodsServices.push(list[index]);
        Object.assign(
          row,
          this.copyWakeUpInfo.serviceItems.use_in_mourn_ceremony[index]
        );
      }
    },

    //删除守灵明细数据
    deleteRow(index, arry) {
      let this_delete = this;
      //定义要删除的数据
      let item = {
        id: arry[index].id,
        version: arry[index].version,
      };
      //先添加删除，后再删除服务物品
      let deleteItems =
        this_delete.copyWakeUpInfo.wakeMourn.charges.deleteIds || [];
      if (arry[index] && item.id != 0) {
        //放入删除数据中
        deleteItems.push(item);
        //放入要要删除的数组中
        this_delete.copyWakeUpInfo.wakeMourn.charges.deleteIds = deleteItems;
      }

      //删除指定数据
      this_delete.copyWakeUpInfo.wakeMourn.charges.goodsServices.splice(
        index,
        1
      );
    },
    mournSelect(item, index) {
      //在弹出选择礼厅对话框中选择
      let this_select = this;
      if (item.deadName && item.businessId > 0) {
        this.$message("礼厅被占用,请选择其他");
        return;
      }
      let selectStart = dataFormate(
        new Date(this_select.copyWakeUpInfo.wakeMourn.appointmentInTime)
      );
      let selectEnd = dataFormate(
        new Date(this_select.copyWakeUpInfo.wakeMourn.appointmentOutTime)
      );
      if (selectStart && selectStart.indexOf("/") != -1) {
        selectStart = selectStart.replace(/\//g, "-");
      }
      if (selectEnd && selectEnd.indexOf("/") != -1) {
        selectEnd = selectEnd.replace(/\//g, "-");
      }
      //替换反斜杠为-
      let timeStart = dataFormate(new Date(item.appointmentInTime));

      let timeEnd = dataFormate(new Date(item.appointmentOutTime));
      if (timeStart && timeStart.indexOf("/") != -1) {
        timeStart = timeStart.replace(/\//g, "-");
      }
      if (timeEnd && timeEnd.indexOf("/") != -1) {
        timeEnd = timeEnd.replace(/\//g, "-");
      }
      let nextInTime = item.nextInTime;
      //开始时间包含在用户选择的时间区间中
      if (
        item &&
        selectStart &&
        selectEnd &&
        timeStart &&
        selectStart < timeStart &&
        timeStart < selectEnd
      ) {
        this.$message("礼厅被占用,请选择其他");
        return;
      } //结束时间包含在用户选择的时间区间中
      else if (
        item &&
        selectStart &&
        selectEnd &&
        timeEnd &&
        selectStart < timeEnd &&
        timeEnd < selectEnd
      ) {
        this.$message("礼厅被占用,请选择其他");
        return;
      } //下一开始时间包含在用户选择的时间区间中
      else if (
        item &&
        selectStart &&
        selectEnd &&
        nextInTime &&
        selectStart < nextInTime &&
        nextInTime < selectEnd
      ) {
        this.$message("礼厅被占用,请选择其他");
        return;
      }
      if (item.disableState == 1) {
        this.$message("礼厅为停用状态");
        return;
      }
      for (let j = 0; j < this.wakeHalls.length; j++) {
        this.wakeHalls[j].flag = false;
      }
      this.wakeHalls[index].flag = !this.wakeHalls[index].flag;
      this.$set(this.wakeHalls, index, this.wakeHalls[index]);
      this.selectname = this.wakeHalls[index];
    },
    hallTypechange(id) {
      //加载守灵礼厅名称
      //带有/的时间字符串
      let startTime = this.copyWakeUpInfo.wakeMourn.appointmentInTime;
      startTime = dataFormate(
        new Date(this.copyWakeUpInfo.wakeMourn.appointmentInTime)
      );
      if (startTime && startTime.indexOf("/") != -1) {
        startTime = startTime.replace(/\//g, "-");
      }
      let endTime = this.copyWakeUpInfo.wakeMourn.appointmentOutTime;
      if (endTime) {
        endTime = dataFormate(
          new Date(this.copyWakeUpInfo.wakeMourn.appointmentOutTime)
        );
      }
      if (endTime && endTime.indexOf("/") != -1) {
        endTime = endTime.replace(/\//g, "-");
      }
      let postData = {
        hallTypeCode: this.copyWakeUpInfo.wakeMourn.hallTypeCode, //选中id
        appointmentInTime: startTime,
        appointmentOutTime: endTime,
      };
      mournService["loadWakeHallWithDate"](postData)
        .then((response) => {
          if (response) {
            let result = response;
            const {data} = result;
            if (!data) {
              this.$message("数据加载失败");
              return;
            }
            //数据读取成功
            this.wakeHalls = data;
            //测试用
            this.centerDialogVisible = true;
          } else {
            this.$message("数据加载失败");
          }
        })
        .catch((error) => {
          this.$message(error);
        });
    },
    //显示选择对话框
    select() {
      if (!this.copyWakeUpInfo.wakeMourn.appointmentInTime) {
        this.$message("请先选择开始时间");
        return;
      }
      if (this.ProcessStateWakeUpOld == 3) {
        this.$message("已出厅，状态锁定");
        return;
      }
      // if (!this.copyWakeUpInfo.wakeMourn.hallTypeCode || !this.copyWakeUpInfo.wakeMourn.appointmentInTime) {
      //     this.$message("请先选择守灵厅及开始时间")
      //     return
      // }
      //判断礼厅是否已禁用
      if (this.ruleItems && this.ruleItems.length > 0) {
        for (let i = 0; i < this.ruleItems.length; i++) {
          let item = this.ruleItems[i];
          if (
            item &&
            item.dataCode == this.copyWakeUpInfo.wakeMourn.hallTypeCode &&
            item.disableState
          ) {
            this.$message("礼厅类型已禁用");
            return;
          }
        }
      }
      //显示名称
      this.hallTypechange(this.copyWakeUpInfo.wakeMourn.hallTypeCode);
    },
    showSelectName() {
      //选择礼厅名称
      // console.log(this.selectname)
      this.selectSave = JSON.parse(JSON.stringify(this.selectname));
      this.copyWakeUpInfo.wakeMourn.hallId = this.selectname.id;

      //  console.log(this.selectSave.hallCode+"uuuuuuuuuuuuuuuuuuuuuuuuu")
      //  console.log(this.selectSave.hallName+"uuuuuuuuuuuuuuuuuuuuuuuuu")
      // console.log(this.ruleItems)
      //     return
      let x = this.ruleItems.find((i) => {
        if (i.dataText === this.selectSave.hallType) {
          return i;
        }
      });
      console.log(x)
      console.log(this.selectSave.hallType)

      //console.log(x.dataCode+"eeeeeeeeeeeeeeeeee")
      this.copyWakeUpInfo.wakeMourn.hallTypeCode = x.dataCode;
      this.changeTypeCodeAsRuleItem(x.dataCode);

      this.centerDialogVisible = false;
    },
    closeDialog() {
      this.$parent.DialogWakeUp = false;
    }, //流程更新状态
    changeWakeUpProcessStateCode(code) {
      this.$forceUpdate();
    },
  },
};
</script>

<style>
.WakeUpDialog .el-dialog__title,
.WakeUpDialog .el-dialog__headerbtn .el-dialog__close {
  color: white;
}

.WakeUpDialog .el-input,
.WakeUpDialog .el-select,
.WakeUpDialog .el-form-item__content {
  width: 180px;
}

.WakeUpDialog .el-radio.is-bordered + .el-radio.is-bordered {
  margin-left: 0px;
}

.WakeUpDialog .el-card__body {
  padding: 20px;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

.WakeUpDialog .el-radio--medium.is-bordered {
  padding: 10px 20px 0 10px;
  border-radius: 4px;
  height: 36px;
  width: 120px;
  flex: 0 0 120px;
  margin-bottom: 20px;
}

.WakeUpDialog .el-radio {
  margin-right: 0px;
}
</style>
<style scoped>
.box-card {
  flex: 0 0 200px;
  display: inline-block;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  margin-bottom: 30px;
  margin-right: 10px;
}

.box-card .item {
  font-size: 16px;
  font-weight: 400;
  line-height: 30px;
  width: 100%;
  height: 90px;
  text-align: left;
  padding: 0 1em;
  border-radius: 5px;
  overflow: hidden;
}

.item:hover {
  cursor: pointer;
}

.item:last-child {
  margin-bottom: 0px;
}

.item.sale {
  background-color: #ff6040;
}

.item.free {
  background: white;
}

.item.repair {
  background: yellow;
}

.item.selected {
  background: lightgreen !important;
}

.item h2 {
  height: 30px;
  overflow: hidden;
}
</style>

